<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @name 		MY_Controller Class
 * @author 		Hyperion-NMV Development
 * @package 	CMS
 * @subpackage 	Core
 */
class MY_Controller extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function strip_json_string($key,$str)
	{
		$pos = strpos($str,$key);
		$tmp = '';
		$loop = true;
		for ($i=$pos;$loop;$i++){
			if (substr($str,$i,1) != ',' && substr($str,$i,1) != '}'){
				$tmp .= substr($str,$i,1);
			}else{
				$loop = false;
			}
		}
		$tmp = str_replace('"',"",$tmp);
		$tmp = explode(':',$tmp);
		$return_val = str_replace('\\','',$tmp[1]);
		return $return_val;
	}

	function upload_file($field_name, $config)
	{
		if(is_array($config))
		{
			if(!$field_name)
			{
				$field_name = 'userfile';
			}

			$this->load->library('upload');
            $this->upload->initialize($config);
			if ( ! $this->upload->do_upload($field_name))
			{
				$response = array(
					'error' => $this->upload->display_errors(),
					'upload_data' => NULL
				);
			}
			else
			{
				$response = array(
					'upload_data' => $this->upload->data(),
					'error' => NULL
				);
			}

			return $response;
		}
		return FALSE;
	}

	function send_mail($fields = array(), $config = NULL)
	{
		$default_fields = array(
			'from_email'		=>	'',
			'from_name'			=>	'',
			'to_email'			=>	'',
			'to_name'			=>	'',
			'cc_email'			=>	'',
			'bcc_email'			=>	'',
			'subject'			=>	'',
			'message'			=>	'',
			'alt_message'		=>	'',
			'attachment'		=>	''

		);

		//overwirting default values
		$fields = array_merge($default_fields, $fields);

	    $this->load->library('email');
	    $this->email->clear();
		if(is_array($config) AND $config)
		{
			$this->email->initialize($config);
		}

	    $this->email->from($fields['from_email'], $fields['from_name']);
	    $this->email->to($fields['to_email'], $fields['to_name']);

		if($fields['cc_email'])
		{
			$this->email->cc($fields['cc_email']);
		}

		if($fields['bcc_email'])
		{
			$this->email->bcc($fields['bcc_email']);
		}

	    $this->email->subject($fields['subject']);
	    $this->email->message($fields['message']);

	    if($fields['alt_message'])
		{
			$this->email->set_alt_message($fields['alt_message']);
		}

		if(isset($fields['attachment']) && $fields['attachment'])
		{
			$this->email->attach($fields['attachment']);
		}

	    if($this->email->send()){
			$return_array = array(
				'send'		=>	TRUE,
				'error'		=>	FALSE
			);
	    }
		else
		{
			$return_array = array(
				'send'		=>	FALSE,
				'error'		=>	$this->email->print_debugger()
			);
		}

	    return $return_array;
	}

	//form_validation class callback using recaptcha
	function check_captcha($val)
	{
		if ($this->recaptcha->check_answer($this->input->ip_address(),$this->input->post('recaptcha_challenge_field'),$val))
		{
			return TRUE;
		}
		else
		{
			$this->form_validation->set_message('check_captcha',$this->lang->line('recaptcha_incorrect_response'));
			return FALSE;
		}
	}

	//getting post variables using $this->input->post()
	function get_post_vars($post)
	{
		if(is_array($post))
		{
			$form_array = array();
			foreach($post as $index)
			{
				$form_array[$index] = $this->input->post($index);
			}

			return $form_array;
		}

		return FALSE;
	}
    
    /********************Grocery CRUD callbacks**************************/
    
    public function _grocerycrud_date_format($value, $row)
    {
        return date('M d, Y',strtotime($value));
    }
    
    public function _grocerycrud_mailto($value, $row)
    {
        $this->load->helper('url');
        return mailto($value,$value);
    }
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */